//
//  SettingTVPresenter.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/8.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit



class SettingTVPresenter: BaseTVPresenter {
    

    
    var webView:UIWebView = {
        return UIWebView.init()
    }()
    
    var actionVC:UIAlertController = {
        
        func StartShared(_ ac:UIAlertAction)
        {
            print(ac)
        }
        
        let c = UIAlertController.init(title: "分享", message: "感谢您的分享", preferredStyle: UIAlertControllerStyle.actionSheet)
        let arr = ["分享给微信好友","分享到微信朋友圈","分享到新浪微博","分享到腾讯微博","分享到QQ"]
        
        for str in arr
        {
            let wxFriend = UIAlertAction.init(title: str, style: .default, handler: { (ac) in
                StartShared(ac)
            })
            c.addAction(wxFriend)
        }

        
        let cancle = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
        c.addAction(cancle)
        
        return c
    }()

    override func setupTableView() {
        super.setupTableView()
        self.contents = ["评分鼓励","退出"]
    }
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? 1 : 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let id = indexPath.section != 1 ? "cell" : "center"
        let index = indexPath.section
        
        
        var cell = tableView.dequeueReusableCell(withIdentifier: id)
        if cell == nil
        {
            
            cell = UITableViewCell.init(style: .default, reuseIdentifier: id)
            cell?.backgroundColor = UIColor.white
            cell?.addLine(WithColor: UIColor.lightGray, model: [.bottom])
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 18)


            if indexPath.section != 1
            {
                cell?.accessoryType = .disclosureIndicator
            }
            else
            {
                cell?.textLabel?.center = (cell?.center)!
                cell?.textLabel?.textAlignment = .center
            }
            cell?.selectionStyle = .none
        }
        cell?.textLabel?.text = self.contents![index + indexPath.row] as? String;
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(self.contents![indexPath.row])
        
        if indexPath.section == 1
        {
            print("安全退出")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: QUIT), object: "logout", userInfo: nil)
        }
        else
        {
            switch indexPath.row {
            case 0:
                let url = URL.init(string: "itms-apps://itunes.apple.com/app/id1181705929")
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(url!, options: [:], completionHandler: nil)
                } else {
                    // Fallback on earlier versions
                    UIApplication.shared.openURL(url!)
                }
//            case 1:
//                self.shared()
//            case 1:
//                let url = URL.init(string: "tel://023-88888888")
//                let urlR = URLRequest.init(url: url!)
//                self.webView.loadRequest(urlR)
//            case 3:
//                print("进入新页面")
//                
//                self.vc.present(IssueVC(), animated: true, completion: nil)
            default:
                break;
            }
        }
    }

    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: 20))
        v.backgroundColor = UIColor.clear
        return v
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    
    func shared()
    {
        self.vc.present(actionVC, animated: true, completion: nil)
    }
    

    
}
